<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#export").click(function (e) {
                e.preventDefault();

                //getting data from our table
                var data_type = 'data:application/vnd.ms-excel';
                var table_div = document.getElementById('report_table');
                var table_html = table_div.outerHTML.replace(/ /g, '%20');

                var a = document.createElement('a');
                a.href = data_type + ', ' + table_html;
                a.download = 'Basic Information Report.xls';
                a.click();
            });
        });

        var submit = function () {

            var month, subproject, district;
            var getMonth = function (a) {
                if (a == "1" || a == "01") { month = "January" }
                if (a == "2" || a == "02") { month = "February" }
                if (a == "3" || a == "03") { month = "March" }
                if (a == "4" || a == "04") { month = "April" }
                if (a == "5" || a == "05") { month = "May" }
                if (a == "6" || a == "06") { month = "June" }
                if (a == "7" || a == "07") { month = "July" }
                if (a == "8" || a == "08") { month = "August" }
                if (a == "9" || a == "09") { month = "September" }
                if (a == "10") { month = "october" }
                if (a == "11") { month = "November" }
                if (a == "12") { month = "December" }
            };
            var json = "";
            var checkOuLevel = function (ou) {
                $.ajax({
                    async: false,
                    type: "GET",
                    url: "../api/organisationUnits/" + ou + ".json",
                    success: function (data) {
                        json = data;

                    }
                });
                return json;
            };


            var getDistrict = function (ou) {
                $.ajax({
                    async: false,
                    type: "GET",
                    url: "../api/organisationUnits/" + ou + ".json?fields=ancestors[id,name,level]",
                    success: function (data) {
                        for (var i = 0; i < data.ancestors.length; i++) {
                            if (data.ancestors[i].level == 2) {
                                district = data.ancestors[i].name;
                            }
                        }

                    }
                });
                return district;
            };
            var year = "", ncbc = "", drcn = "", csd = "", ced = "", cid = "", noc = "", cf = "", ct = "", length = "", ps = "", ca = "", eea = "", sub = "", pgt = "", agt = "", cd = "";
            var url = window.location.href;
            // var pe1 = url.split("&")[1];
            // var pe = pe1.split("=")[1];

            var startDat = document.getElementById("start").value;
            var endDat = document.getElementById("end").value;

            var s = startDat.split('-');
            var ss = s.join('');
            var startmonth = ss.substr(0, 6);
            var e = endDat.split('-');
            var ee = e.join('');
            var endmonth = ee.substr(0, 6);

            //  var monthCode = endmonth.substr(4, 6);
            // getMonth(monthCode);
            //  year = pe.substr(0, 4)
            var ou1 = url.split("&")[1];
            var ou = ou1.split("=")[1];
            var ouLeveldata = checkOuLevel(ou);
            var ouLevel = ouLeveldata.level;
            var printData = function (ouu) {
                $.getJSON("../api/26/analytics.json?dimension=dx:nQto4teC0cQ;PBwwj18Kj4I;cpdxqSw0QKv;hIrTYge0ZZz;CdpqLMQc1c7;GXBIWA6Gufu;PlM082ZGsEi;SkWffXvOtNd;hIrTYge0ZZz;isGe8SeEEdw;Lu3oCmYRwEr;rsekYnlKyNZ;N5fSrVUZmah;Wwck92FsZKR;pZuxxyBA3wr;Si8rRIkCddA;XD6mqK5pjrn&dimension=ou:" + ouu + "&filter=pe:" + startmonth + ";" + endmonth + "&displayProperty=NAME&outputIdScheme=NAME", function (data) {
                    var districtt = getDistrict(ouu);
                    for (var i = 0; i < data.rows.length; i++) {
                        if (data.rows[i][0] == "Basic information_Chainage from") {
                            cf = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Contract name") {
                            ncbc = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Contract ID") {
                            cid = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Chainage to") {
                            ct = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Engineer s estimate amount") {
                            eea = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Contractor name") {
                            noc = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_DRCN code") {
                            drcn = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Propsed surfacing") {
                            ps = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Contract amount") {
                            ca = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Length (kms)") {
                            length = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Contract end date") {
                            ced = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Contract start date") {
                            csd = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic Information_ performance guarantee time ") {
                            pgt = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_advance guarantee time") {
                            agt = data.rows[i][2];
                        }
                        if (data.rows[i][0] == "Basic information_Contract duration") {
                            cd = data.rows[i][2];
                        }
                        sub = data.rows[i][1];
                    }
                    if (data.rows.length == 0) { }
                    else {
                        var row = "<tr><td>" + districtt + "</td><td>" + sub + "</td><td>" + drcn + "</td><td>" + ncbc + "</td><td>" + cid + "</td><td>" + noc + "</td><td>" + csd + "</td><td>" + ced + "</td><td>" + cd + "</td><td>" + ca + "</td><td>" + cf + "</td><td>" + ct + "</td><td>" + eea + "</td><td>" + length + "</td><td>" + agt + "</td><td>" + pgt + "</td><td>" + ps + "</td></tr>";
                        $(".reporttable").append(row);
                    }
                });
            };

            if (ouLevel == 1) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            var tempOu2 = tempOuData.children[l].id;
                            var tempOuData2 = checkOuLevel(tempOu2);
                            if (tempOuData2.children.length !== 0) {
                                for (var m = 0; m < tempOuData2.children.length; m++) {
                                    ouu = tempOuData2.children[m].id;
                                    printData(ouu);

                                }
                            }
                        }
                    }
                }
            }

            else if (ouLevel == 2) {

                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            ouu = tempOuData.children[l].id;
                            printData(ouu);
                        }
                    }
                }
            }
            else if (ouLevel == 3) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    ouu = ouLeveldata.children[p].id;
                    printData(ouu);
                }
            }
            else {
                printData(ou);
            }
        };
    </script>
    <style>
        .heading td {

            color: white;
            text-align: center;
            border: none;
        }

        #heading {
            text-align: center;
        }


        table {
            border-collapse: collapse;
        }

        td {
            border: 1px solid black;
            height: 30px;
            width: 280px;
            text-align: center;
            font-weight: bold;
        }

        .noBorder {
            border: none;
        }

        table thead {
            background: lightgray;
        }

        #export {
            margin-left: 90px;
        }
    </style>
</head>

<body>

    <div class="body">
        <button id="export">Export to Excel</button>
        <br>

        <center> Start Date :
            <input type="date" id="start" required> End Date :
            <input type="date" id="end" required><br><br>
            <button onClick="submit()" id="tap">Submit</button></center><br><br>
        <table class="table table-bordered reporttable" id="report_table" border='1'>
            <thead>
                <tr class="heading" bgcolor="#1D9FDF" border="none" text-align="center">
                    <td colspan="3" rowspan="3" border="none" text-align="center"><img src="../dhis-web-commons/security/DRILP_Logo.jpg" width="90%" height="400%"></td>
                    <td colspan="11" rowspan="1" border="none" text-align="center">
                        <p style="font-size:16px;">Earthquake Emergency Assistance Project (EEAP)</p>
                    </td>
                    <td colspan="3" rowspan="3" border="none" text-align="center"></td>
                </tr>
                <tr class="heading" bgcolor="#1D9FDF">
                    <td colspan="11" rowspan="1" border="none" text-align="center">
                        <p style="font-size:20px;">Rehabilitation and Reconstruction of District Roads</p>
                    </td>
                </tr>
                <tr class="heading" bgcolor="#1D9FDF" border="none" text-align="center">
                    <td colspan="11" rowspan="1" border="none" text-align="center">
                        <p style="font-size:16px;">Monthly Project Progress Report (Part-I)</p>
                    </td>
                </tr>

                <tr>
                    <td>District</td>
                    <td>Sub-project</td>
                    <td>DRCN code</td>
                    <td>Contract name</td>
                    <td>Contract ID</td>
                    <td>Contractor name</td>
                    <td>Contract start date</td>
                    <td>Contract end date</td>
                    <td>Contract duration</td>
                    <td>Contract amount</td>
                    <td>Chainage from</td>
                    <td>Chainage to</td>
                    <td>Engineer's Estimate amount</td>
                    <td>Length(kms)</td>
                    <td>Advance guarantee time</td>
                    <td>Performance guarantee time</td>
                    <td>Proposed surfacing</td>
                </tr>
            </thead>
        </table>

    </div>
</body>

</html>